import * as constants from './constants'
import axios from 'axios'
import { fromJS } from 'immutable'

// 初始化搜索列表
const InitSearchList = (list) => ({
  type: constants.GET_SEARCH_LIST,
  list: fromJS(list),
  totalPage: Math.ceil(list.length / 5)
})
export const searchFocus = () => ({
  type: constants.SEARCH_FOCUS
})
export const searchBlur = () => ({
  type: constants.SEARCH_BLUR
})
// 动作 ：获取搜索列表内容
export const getSearchList = () => {
  // 使用 redux-thunk中间件 可以返回一个函数 该函数 可以接受dispatch
  return (dispatch) => {
    console.log('派发搜索列表动作')
    axios.get('api/list.json').then((res) => {
      if (res.data.status === 200) {
        console.log('数据获取成功，派发初始化搜索列表')
        dispatch(InitSearchList(res.data.data))
      }
    }).catch((err) => {
      console.log(err)
    })
  }
}
export const mouseEnter = () => ({
  type: constants.ON_MOUSE_ENTER
})
export const mouseLeave = () => ({
  type: constants.ON_MOUSE_LEAVE
})
// 搜索页码改变
export const changePageNum = (pageNum) => ({
  type: constants.Change_Page_Num,
  pageNum
})